{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "参考：[Python 3.10增加了比switch更强大的match表达式](https://www.bilibili.com/video/BV1C3411Y7eq)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def switch(case):\n",
    "    mapping = {\n",
    "        1: \"print('case 1')\",\n",
    "        2: \"print('case 2')\"\n",
    "    }\n",
    "    return eval(mapping[case])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case 1\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "\n",
    "switch(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case 1\n"
     ]
    }
   ],
   "source": [
    "a = 1\n",
    "\n",
    "match a:\n",
    "    case 1:\n",
    "        print('case 1')\n",
    "    case 2:\n",
    "        print('case 2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 3\n",
    "\n",
    "match a:\n",
    "    case 1:\n",
    "        print('case 1')\n",
    "    case 2:\n",
    "        print('case 2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case default 3\n"
     ]
    }
   ],
   "source": [
    "a = 3\n",
    "\n",
    "match a:\n",
    "    case 1:\n",
    "        print('case 1')\n",
    "    case 2:\n",
    "        print('case 2')\n",
    "    case default:\n",
    "        print('case default', default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case 1\n"
     ]
    }
   ],
   "source": [
    "a = (1, 2)\n",
    "\n",
    "match a:\n",
    "    case (1, 2):\n",
    "        print('case 1')\n",
    "    case (2, x):\n",
    "        print('case 2', x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case 2 4\n"
     ]
    }
   ],
   "source": [
    "a = (2, 4)\n",
    "\n",
    "match a:\n",
    "    case (1, 2):\n",
    "        print('case 1')\n",
    "    case (2, x):\n",
    "        print('case 2', x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case 2 2\n"
     ]
    }
   ],
   "source": [
    "a = (2, 2)\n",
    "flag = True\n",
    "\n",
    "match a:\n",
    "    case (1, 2):\n",
    "        print('case 1')\n",
    "    case (2, x) if flag:\n",
    "        print('case 2', x)\n",
    "    case default:\n",
    "        print('case default', default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case default (2, 2)\n"
     ]
    }
   ],
   "source": [
    "a = (2, 2)\n",
    "flag = False\n",
    "\n",
    "match a:\n",
    "    case (1, 2):\n",
    "        print('case 1')\n",
    "    case (2, x) if flag:\n",
    "        print('case 2', x)\n",
    "    case default:\n",
    "        print('case default', default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "f818d045086a6019c45e763e00262af1a3664e70820d43d3d165b72dd6cc00d2"
  },
  "kernelspec": {
   "display_name": "Python 3.10.0 64-bit ('env_310': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
